<?php  
class Model_User extends Model{ 

    public function register($user_name,$user_email,$user_password){

    	$user_total= DB::select(array(DB::expr('COUNT(1)'), 'total'))->from('site_user')->where("fullname",'=',$user_name)->limit(1)->execute()->get("total");
    	
      $error_info=array();

      $result=array();

      $email_total= DB::select(array(DB::expr('COUNT(1)'), 'total'))->from('site_user')->where("user_email",'=',$user_email)->limit(1)->execute()->get("total");

      if($user_total=="0"&&$email_total=="0"){

        try{

          list($insert_id, $total_rows)= DB::insert('site_user')

          ->columns(array('fullname','user_email','user_pwd','createdon'))

          ->values(array($user_name,$user_email,sha1($user_password),date("Y-m-d h:i:sa")))->execute();
            
          Database::instance()->commit();

          $user=array(

            'userid'=>$insert_id,

            'username'=>$user_name
          );

          Session::instance()->set('user',$user);

          $result["code"]="0x00000";

        }

        catch (Exception $e) {

          Database::instance()->rollback();

          $result["code"]="0x00001";

          $result["msg"]=__("Sorry,server is busy,please try it again");

        }
    	}
    	else{

          if($user_total>0){

            $error_info[]=array(

              'reg_username'=>__("Sorry, the username already exists")

            );
          }

          if($email_total>0){
        
            $error_info[]=array(

              'reg_email'=>__("Sorry, the email already exists")

            );
          }

          $result['code']="001";

    	}

      return json_encode($result);

    }

   public function user_login($login_user,$user_pwd){

      $count=DB::select('site_userid')->from('site_user')->where('user_email', '=', $login_user)->or_where('fullname', '=', $login_user)->limit(1)->execute()->count();

      if($count>0){

            $sql="select site_userid,fullname,photo from  site_user

            where (user_email='$login_user' or fullname='$login_user')

            and user_pwd='$user_pwd'";

            $query=DB::query(Database::SELECT,$sql)->execute();

            $count=$query->count();

            if($count>0){

                $arr= array('code'=>'0x00000');

                $user=array(
                    'userid'=>$query->get("site_userid"),
                    'username'=>$query->get("fullname"),
                    'photo'=>$query->get("photo")
                );

                Session::instance()->set('user',$user);

                $arr['user_name']=$query->get("fullname");

                return json_encode($arr);
            }
            else{

              return json_encode(array("code"=>"001","msg"=>__("Sorry, the password is incorrect")));

            }
      }
      else{

         return json_encode(array("code"=>"001","msg"=>__("Sorry, the email or username is not exists")));

      }
   }

   public function saveLogin($userid,$old_password,$new_password){

      $result=array();

      $count=DB::select('site_userid')->from('site_user')->where('user_pwd', '=', sha1($old_password))->and_where('site_userid','=',$userid)->limit(1)->execute()->count();

      if($count>0){

          $pwd=sha1($new_password);

          try {

            Database::instance()->begin();

            $total_rows=DB::update('site_user')

            ->set(array('user_pwd'=>$pwd))

            ->where('site_userid','=',$userid)->execute();

            Database::instance()->commit();

            $result['code']="0x00000";

          }

          catch (Exception $e) {

            Database::instance()->rollback();

            $result['code']="0x0000x";

            $result['info']=__("Sorry,server is busy,please try it again");

          }

      }
      else{

        $result['code']="0x0000x";

        $result['info']=__("Old password is incorrect");

      }

      return $result;

   }

   public function getInfoByUserName($username){

      $sql="select t.site_userid userid,t.fullname,t.photo
            from site_user t
            where t.isdeleted<>'1'
            and t.fullname='$username'
            limit 1";

      $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

      if(!empty($data)){

          return $data[0];
      }

      return $data;

   }

   public function getInfoByUserId($userid){

      $sql="select t.site_userid userid,t.fullname,t.photo,t.user_email email,t.about
            from site_user t
            where t.isdeleted<>'1'
            and t.site_userid='$userid'
            limit 1";

      $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

      if(!empty($data)){

          return $data[0];
      }

      return $data;

   }

}
